﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class NhanVienListView : Form
    {
        private BindingList<NhanVien> bindingListNhanVien;
        private NhanVien _nhanVien;

        public NhanVienListView()
        {
            InitializeComponent();
        }

        private void NhanVienListView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[]{"MaQuanLy","Ho","Ten","GioiTinh","NgaySinh","NoiSinh","QueQuan","BoPhan.TenBoPhan","TinhTrang.TenTinhTrang","ChucVu.TenChucVu"};
            string[] listColumnName = new string[]{"Mã quản lý","Họ","Tên","Giới tính","Ngày sinh","Nơi sinh","Quê quán","Bộ Phận","Tình Trạng","Chức Vụ"};
            AppGridView.InitGridView(gvNhanVien, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvNhanVien);
            AppGridView.ShowField(gvNhanVien, listFieldName, listColumnName );
            LoadData();
        }
        private void LoadData()
        {
            bindingListNhanVien = new BindingList<NhanVien>(DataAccessObject.NhanVien.GetListNhanVien());

            bindingSourceNhanVien.DataSource = bindingListNhanVien;
            gvNhanVien.Invalidate();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void gcNhanVien_DoubleClick(object sender, EventArgs e)
        {
            _nhanVien = gvNhanVien.GetFocusedRow() as NhanVien;
            OpenDetailForm();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _nhanVien = new NhanVien();
            OpenDetailForm();
        }

        private void OpenDetailForm()
        {
            NhanVienDetailView detailView = new NhanVienDetailView(_nhanVien);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.MdiParent = this.MdiParent;
            detailView.Show();
            
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            gvNhanVien.Invalidate();
        }
    }
}
